
import api from "./../../../common/api";
import utils from './../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cooperationId: "", //当前电话中的合作id
    isVmShow: false, //虚拟电话弹层
    hasMoreList: true,
    id: '',
    pageIndex: 0,
    pageSize: 20,
    historyList: [],
    toUserId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().globalData.loginPromise.then(() => {
      let t = this
      t.initData(options)
      t.loadHistoryList()
    })
  },

  handleCopyPhoneNum(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.wxMobile,
      success: () => {
        wx.showToast({
          title: '手机号复制成功',
        })
      }
    })
  },

  handleVmVisible(e) {
    this.setData({
      isVmShow: e.detail.visible,
      cooperationId: e.detail.cooperationId,
    });
  },

  onPhone(e) {
    const model = e.detail;
    this.setData({
      isVmShow: true,
      cooperationId: model.cooperationId,
      toUserId:model.toUserId
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let t = this
    t.loadHistoryList()
  },

  initData(options) {
    let t = this
    t.myData = {}
    t.setData({
      id: options.id || ''
    })
  },

  loadHistoryList() {
    let t = this
    return new Promise((resolve, reject) => {
      let {
        id,
        pageIndex,
        pageSize,
        hasMoreList,
      } = t.data
      if (!hasMoreList) {
        resolve()
        return
      }
      wx.showLoading({
        title: '加载中...',
      })
      let currentIndex = pageIndex + 1
      api.myCooperationTalkingDetail({
        cooperationId: id,
        pageNum: currentIndex,
        pageSize,
      }, (res) => {
        wx.hideLoading()
        if (res && res.data && res.data.isSuccess) {
          let result = res.data.content || {}
          let list = result.list || []
          let maxIndex = result.lastPage
          let formatList = list.map(item => {
            return {
              ...item,
              avatar: item.avatar || '/images/default_head.png',
              name: item.isVerified ? item.realName || '' : item.nickName || '',
              time: utils.dateFormat(new Date(item.lastCommunicateTime || item.talkingTime), 'yyyy-MM-dd hh:mm') || '',
              status: item.copyHouseStatus,
              unionId: item.unionId || '',
            }
          })
          t.data.historyList.push(...formatList)
          t.setData({
            pageIndex: currentIndex,
            hasMoreList: currentIndex >= maxIndex ? false : true,
            historyList: t.data.historyList
          })
          resolve()
        } else {
          reject()
        }
      }, () => {
        reject()
        wx.hideLoading()
      })
    })
  },

  onJumpChat(e) {
    let unionId = e.currentTarget.dataset.unionid || ''
    wx.navigateTo({
      url: `/pages/im/imMessagelist/imMessagelist?unionid=${unionId}&pageFrom=房源咨询记录页`,
    })
  }
})